package com.cloudspace.jindun.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.cloudspace.jindun.JindunApp;
import com.cloudspace.jindun.R;
import com.cloudspace.jindun.config.Configs;
import com.cloudspace.jindun.model.Person;
import com.cloudspace.jindun.net.manager.ImageManager;
import com.cloudspace.jindun.utils.ObjUtil;

import java.util.Collections;
import java.util.List;

/**
 * Created by zengxianhua on 16/12/25.
 */

public class PersonContactAdatper extends BaseAdapter{
    private List<Person> data;

    @Override
    public int getCount() {
        if (ObjUtil.isEmpty(data)){
            return 0;
        }

        return data.size();
    }

    public void setData(List<Person> datas){
        if (ObjUtil.isEmpty(datas)){
            return;
        }

        Collections.sort(datas);
        this.data = datas;
        notifyDataSetChanged();
    }

    @Override
    public Object getItem(int position) {
        if (ObjUtil.isEmpty(data)){
            return null;
        }
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(JindunApp.getInstance()).inflate(R.layout.item_dialing_people, null);
            holder.tv_company = (TextView) convertView.findViewById(R.id.phone_textview_company);
            holder.tv_name = (TextView) convertView.findViewById(R.id.phone_textview_name);
            holder.tv_number= (TextView) convertView.findViewById(R.id.phone_textview_number);
            holder.headIcon = (ImageView) convertView.findViewById(R.id.phone_image_header);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tv_company.setText(data.get(position).getCompany());
        holder.tv_name.setText(data.get(position).getName());
        holder.tv_number.setText(data.get(position).getPhoneNumber());

        ImageManager.loadCircleImage(JindunApp.getInstance(), holder.headIcon, data.get(position).getImageUrl(), Configs.IMAGE_CORNER_RADIUS);
        return convertView;
    }

    static class ViewHolder {
        private TextView tv_company;
        private TextView tv_name;
        private TextView tv_number;
        private ImageView headIcon;
    }
}
